package com.ganbo.ms.java8.stream;

import com.ganbo.ms.java8.sort.Person;
import lombok.val;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ganbo
 * @date 2022/12/14 21:39
 */
public class Demo1Intro {

    public static void main(String[] args) {

//        ArrayList<String> list = new ArrayList<>();
//        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰");
//
//        //拿到所有姓张的名字，拿到所有长度为3的名字，打印
//        list.stream().filter((o) -> o.startsWith("张")).filter((o -> o.length() == 3)).forEach(System.out::print);
//        Set<String> z = list.stream().filter((o) -> o.startsWith("张")).filter((o -> o.length() == 3)).collect(Collectors.toSet());
//        System.out.println(z + "     " + list);
//
//        final long count = list.stream().count();
//        System.out.println(count);
//
//        list.stream().forEach(System.out::println);
//
//
//        list.stream().filter(o -> o.startsWith("张")).filter(o -> o.length() == 3).collect(Collectors.toSet());
//
//
//        final long count1 = Stream.of("aaa", "bbb", "ccc").filter(o -> o.startsWith("d")).count();
//        System.out.println(count1);
//
//        final long count2 = list.stream().skip(1).count();
//        System.out.println(count2);


        List<Person> list2 = new ArrayList<>();
        list2.add(new Person(1, "aaa"));
        list2.add(new Person(6, "bbb"));
        list2.add(new Person(5, "ccc"));
        list2.add(new Person(7, "ddd"));

        System.out.println(list2);
//        list2.stream().limit(3).collect(Collectors.toList()).forEach(System.out::print);
//        System.out.println();
//        list2.stream().skip(1).collect(Collectors.toList()).forEach(System.out::print);
//        System.out.println();
//        final List<String> collect = list2.stream().map(Person::getName).collect(Collectors.toList());
//        System.out.println(collect);
//
//        list2.stream().sorted(Comparator.comparingInt(Person::getId)).map(Person::getId).forEach(System.out::print);
//
//        list2.stream().distinct().forEach(System.out::print);
//
//        final boolean b = list2.stream().allMatch(o -> o.getId() > 0);
//        System.out.println(b);

        final Person person = list2.stream().findFirst().get();
        final Person person1 = list2.stream().findAny().get();

        System.out.println(person + "," + person1);

        final Optional<Person> max = list2.stream().max((o1, o2) -> o1.getId() - o2.getId());
        System.out.println(max);

        final Integer reduce = Stream.of(3, 4, 5, 6, 7, 8).reduce(0, (x, y) -> {
            System.out.println("x==>" + x + ", y: " + y);
            return x + y;
        });

        System.out.println(reduce);

        final val reduce1 = Stream.of("a", "c", "b", "a", "b", "a")
                .map(o -> {
                    if (o == "a") return 1;
                    else return 0;
                }).reduce(0, Integer::sum);
        System.out.println(reduce1);

    }
}
